﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace School.UserCentents
{
    public partial class GroupClass : UserCheckPower
    {
        #region 分页相关

        //获取传入当前页码
        int PageIndex
        {
            get
            {
                if (Request.QueryString.Get("page") != null)
                {
                    if (!Common.TypeValidator.IsNumeric(Request.QueryString.Get("page")))
                    {
                        return 1;
                    }
                    return Convert.ToInt32(Request.QueryString.Get("page"));
                }
                return 1;
            }
        }

        //分页数
        private int PageSize
        {
            get
            {
                return 5;
            }
        }
        #endregion
        #region 传递参数
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get
            {
                if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["id"].ToString());
            }
        }
        public int Str1
        {
            get
            {
                if (Request.QueryString["str1"] == null || Request.QueryString["str1"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["str1"].ToString());
            }
        }
        public int Str2
        {
            get
            {
                if (Request.QueryString["str2"] == null || Request.QueryString["str2"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["str2"].ToString());
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.UserType UserType = new BLL.UserType();
        BLL.Classes Classes = new BLL.Classes();
        BLL.ClassPersonnel cp = new BLL.ClassPersonnel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 3)
            {
                Panel p1 = (Panel)Master.FindControl("Panel1");
                Panel p2 = (Panel)Master.FindControl("Panel2");
                Panel p3 = (Panel)Master.FindControl("Panel3");
                Panel p4 = (Panel)Master.FindControl("Panel4");
                p1.CssClass = "sldmenuoff";
                p2.CssClass = "sldmenuon0";
                p3.CssClass = "sld1";
                p4.CssClass = "sld0";
                p3.Attributes.Add("style", "display: none");
                p4.Attributes.Add("style", "");
            }
            if (!IsPostBack)
            {
                ViewState["str1"] = 1;
                PageLoad();
                Repeater1.DataSource = cp.GetList("Manager!='' and ClassID=" + ID);
                Repeater1.DataBind();
            }
        }

        private void PageLoad()
        {
            int a = 4;
            Model.Users u = new BLL.Users().GetModel(LoginUserId);
            if (u.UserTypeID != 4)
            {
                Repeater2.DataSource = UserType.GetList("ID in (4,5,6)");
                Repeater2.DataBind();
            }
            else
            {
                a = 5;
                Repeater2.DataSource = UserType.GetList("ID in (5,6)");
                Repeater2.DataBind();
            }

            string strWhere = " and UserTypeID=" + a + " and CreateUserID=" + LoginUserId;
            DataSet ds = Users.GetList(" UserTypeID=" + a + " and CreateUserID=" + LoginUserId);
            if (u.UserTypeID == 1)
            {
                strWhere = " and UserTypeID=" + a + "";
                ds = Users.GetList("UserTypeID=" + a + " ");
            }
            else if (u.UserTypeID == 2)
            {
                strWhere = " and UserTypeID=" + a + " and Remark=" + LoginUserId;
                ds = Users.GetList("UserTypeID=" + a + " and Remark=" + LoginUserId);
            }
            else if (u.UserTypeID == 3)
            {
                strWhere = " and UserTypeID=" + a + " and Remark2=" + LoginUserId;
                ds = Users.GetList("UserTypeID=" + a + " and Remark2=" + LoginUserId);
            }
            else if (u.UserTypeID == 4)
            {
                string s = "";
                DataSet ds2 = cp.GetList("Manager!='' and ClassID=" + ID);
                if (ds2.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        s += ds2.Tables[0].Rows[i]["Manager"].ToString() + ",";
                    }
                    strWhere = " and UserTypeID=" + a + " and CreateUserID in (" + s.TrimEnd(',') + ")";
                    ds = Users.GetList("UserTypeID=" + a + " and CreateUserID in (" + s.TrimEnd(',') + ") ");
                }
            }

            Repeater3.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater3.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 5;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                if (ID > 0)
                {
                    if (ViewState["str1"] != null)
                    {
                        if (ViewState["str1"].ToString() == "1")
                        {
                            DataSet ds = cp.GetList("Manager=" + e.CommandArgument + " and ClassID=" + ID);
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                cp.Delete(int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()));
                            }
                            Repeater1.DataSource = cp.GetList("Manager!='' and ClassID=" + ID);
                            Repeater1.DataBind();
                        }
                        else
                        {
                            DataSet ds = cp.GetList("UserID=" + e.CommandArgument + " and ClassID=" + ID);
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                cp.Delete(int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()));
                            }
                            Repeater1.DataSource = cp.GetList("UserID!='' and ClassID=" + ID);
                            Repeater1.DataBind();
                        }
                    }
                }
            }
        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex == 0)
                {
                    LinkButton lb = (LinkButton)e.Item.FindControl("LinkButton3");
                    lb.CssClass = "selected";
                }
            }
        }

        protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Check")
            {
                foreach (RepeaterItem rs in Repeater2.Items)
                {
                    LinkButton lk = (LinkButton)rs.FindControl("LinkButton3");
                    lk.CssClass = "";
                }
                LinkButton lb = (LinkButton)e.Item.FindControl("LinkButton3");
                lb.CssClass = "selected";

                Model.Users u = new BLL.Users().GetModel(LoginUserId);

                string strWhere = " and UserTypeID=" + e.CommandArgument + " and CreateUserID=" + LoginUserId;
                DataSet ds = Users.GetList(" UserTypeID=" + e.CommandArgument + " and CreateUserID=" + LoginUserId);
                if (u.UserTypeID == 1)
                {
                    strWhere = " and UserTypeID=" + e.CommandArgument + "";
                    ds = Users.GetList("UserTypeID=" + e.CommandArgument);
                }
                else if (u.UserTypeID == 2)
                {
                    strWhere = " and UserTypeID=" + e.CommandArgument + " and Remark=" + LoginUserId;
                    ds = Users.GetList("UserTypeID=" + e.CommandArgument + " and Remark=" + LoginUserId);
                }
                else if (u.UserTypeID == 3)
                {
                    strWhere = " and UserTypeID=" + e.CommandArgument + " and Remark2=" + LoginUserId;
                    ds = Users.GetList("UserTypeID=" + e.CommandArgument + " and Remark2=" + LoginUserId);
                }
                else if (u.UserTypeID == 4)
                {
                    string s = "";
                    DataSet ds2 = cp.GetList("Manager!='' and ClassID=" + ID);
                    if (ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                        {
                            s += ds2.Tables[0].Rows[i]["Manager"].ToString() + ",";
                        }
                        strWhere = " and UserTypeID=" + e.CommandArgument + " and CreateUserID in (" + s.TrimEnd(',') + ")";
                        ds = Users.GetList("UserTypeID=" + e.CommandArgument + " and CreateUserID in (" + s.TrimEnd(',') + ") ");
                    }
                }

                Repeater3.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
                Repeater3.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 5;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
        }

        protected void Repeater3_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                if (ID > 0)
                {
                    if (ViewState["str1"] != null)
                    {
                        if (ViewState["str1"].ToString() == "1")
                        {
                            DataSet ds = cp.GetList("Manager=" + e.CommandArgument + " and ClassID=" + ID);
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                if (ds.Tables[0].Rows[i]["Manager"].ToString() == e.CommandArgument.ToString())
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('已存在，不可重复增加！');</script>", false);
                                    return;
                                }
                            }
                            ds = cp.GetList("UserID=" + e.CommandArgument + " and ClassID=" + ID);
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                cp.Delete(int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()));
                            }
                            Model.ClassPersonnel per = new Model.ClassPersonnel();
                            per.Manager = int.Parse(e.CommandArgument.ToString());
                            per.ClassID = ID;
                            cp.Add(per);
                            Repeater1.DataSource = cp.GetList("Manager!='' and ClassID=" + ID);
                            Repeater1.DataBind();

                        }
                        else
                        {
                            DataSet ds = cp.GetList("UserID=" + e.CommandArgument + " and ClassID=" + ID);
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                if (ds.Tables[0].Rows[i]["UserID"].ToString() == e.CommandArgument.ToString())
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('已存在，不可重复增加！');</script>", false);
                                    return;
                                }
                            }
                            ds = cp.GetList("Manager=" + e.CommandArgument + " and ClassID=" + ID);
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('此用户已是管理员，不可重复增加！');</script>", false);
                                return;
                            }
                            Model.ClassPersonnel per = new Model.ClassPersonnel();
                            per.UserID = int.Parse(e.CommandArgument.ToString());
                            per.ClassID = ID;
                            cp.Add(per);
                            Repeater1.DataSource = cp.GetList("UserID!='' and ClassID=" + ID);
                            Repeater1.DataBind();
                        }
                    }
                }
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (ViewState["str1"] != null)
                {
                    if (ViewState["str1"].ToString() == "0")
                    {
                        Label lbUserID = (Label)e.Item.FindControl("lbUserID");
                        LinkButton lkUserID = (LinkButton)e.Item.FindControl("lkUserID");
                        Model.Users u = Users.GetModel(int.Parse(lbUserID.Text));
                        lbUserID.Text = u.MemberName + "(" + u.MembeLoginName + ")";
                        lbUserID.Visible = lkUserID.Visible = true;
                    }
                    else
                    {
                        Label lbUserManager = (Label)e.Item.FindControl("lbUserManager");
                        LinkButton lkManager = (LinkButton)e.Item.FindControl("lkManager");
                        Model.Users u = Users.GetModel(int.Parse(lbUserManager.Text));
                        lbUserManager.Text = u.MemberName + "(" + u.MembeLoginName + ")";
                        lbUserManager.Visible = lkManager.Visible = true;
                    }
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ViewState["str1"] = 1;
            lbTitle.Text = "管理员列表";
            lbSTlele.Text = "添加管理员";
            LinkButton1.CssClass = "btn_hover";
            LinkButton2.CssClass = "btn";

            Repeater1.DataSource = cp.GetList("Manager!='' and ClassID=" + ID);
            Repeater1.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            ViewState["str1"] = 0;
            lbTitle.Text = "成员列表";
            lbSTlele.Text = "添加成员";
            LinkButton1.CssClass = "btn";
            LinkButton2.CssClass = "btn_hover";

            Repeater1.DataSource = cp.GetList("UserID!='' and ClassID=" + ID);
            Repeater1.DataBind();
        }

    }
}